﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using Autodesk.Revit.DB;
using YArchitech.LIB.YJKControls;

namespace YJKFilterExpanded.YJKFilterExpanded
{
	public partial class ExtendedFilterForm : System.Windows.Forms.Form
	{
		public ExtendedFilterForm(FilterManager manager, List<CategoryItemData> selectCategoryList)
		{
			this.m_Manager = manager;
			this.m_SelectCategoryList = selectCategoryList;
			this.InitializeComponent();
			this.rbFamilyType.Checked = true;
			this.treeListView.HideSelection = false;
		}

		public bool ZoomToElements
		{
			get
			{
				return this.chbZoom.Checked;
			}
		}

		private void ExtendedFilterForm_Load(object sender, EventArgs e)
		{
			this.UpdateFilterTypeChange();
		}

		private void ExtendedFilterForm_FormClosed(object sender, FormClosedEventArgs e)
		{
		}

		private void btnSelAll_Click(object sender, EventArgs e)
		{
			foreach (object obj in this.treeListView.Items)
			{
				((TreeListViewItem)obj).Checked = true;
			}
		}

		private void btnSelNone_Click(object sender, EventArgs e)
		{
			foreach (object obj in this.treeListView.Items)
			{
				((TreeListViewItem)obj).Checked = false;
			}
		}

		private void btnExpandAll_Click(object sender, EventArgs e)
		{
			this.treeListView.ExpandAll();
		}

		private void btnCollapseAll_Click(object sender, EventArgs e)
		{
			this.treeListView.CollapseAll();
		}

		private void btnUpdateList_Click(object sender, EventArgs e)
		{
			if (this.dbgPropertiesList.RowCount == 0 || this.m_CurrentSelectedItem == null)
			{
				return;
			}
			if (this.m_CurrentSelectedItem.Level != 1)
			{
				return;
			}
			TreeListViewItem currentSelectedItem = this.m_CurrentSelectedItem;
			FamilyItemData familyItemData = this.GetItemDataByTreeNode(currentSelectedItem) as FamilyItemData;
			List<Element> allElements = familyItemData.GetAllElements();
			List<PropertyInfo> thePropertyInfo = familyItemData.ThePropertyInfo;
			List<GroupingDataByProperty> groupingDataList = new List<GroupingDataByProperty>();
			this.m_Manager.GroupingByProperties(allElements, thePropertyInfo, ref groupingDataList);
			familyItemData.ResetGroupingDataList(groupingDataList);
			this.UpdateFilterTypeChange();
			this.UpdateSelectCount();
			this.UpdateCurrentSelectedItem();
		}

		private void UpdateCurrentSelectedItem()
		{
			if (this.m_CurrentSelectedItem == null)
			{
				return;
			}
			bool flag = false;
			CollectionBase visibleItems = this.treeListView.GetVisibleItems();
			string text = this.m_CurrentSelectedItem.Tag.ToString();
			foreach (object obj in visibleItems)
			{
				TreeListViewItem treeListViewItem = (TreeListViewItem)obj;
				if (treeListViewItem.Level == this.m_CurrentSelectedItem.Level)
				{
					if (treeListViewItem.Level == 0)
					{
						if (text.CompareTo(treeListViewItem.Tag.ToString()) != 0)
						{
							continue;
						}
					}
					else if (treeListViewItem.Level == 1)
					{
						string text2 = this.m_CurrentSelectedItem.Parent.Tag.ToString();
						if (text.CompareTo(treeListViewItem.Tag.ToString()) != 0)
						{
							continue;
						}
						if (text2.CompareTo(treeListViewItem.Parent.Tag.ToString()) != 0)
						{
							continue;
						}
					}
					else
					{
						string text3 = this.m_CurrentSelectedItem.Parent.Parent.Tag.ToString();
						string text4 = this.m_CurrentSelectedItem.Parent.Tag.ToString();
						if (text.CompareTo(treeListViewItem.Tag.ToString()) != 0 || text4.CompareTo(treeListViewItem.Parent.Tag.ToString()) != 0 || text3.CompareTo(treeListViewItem.Parent.Parent.Tag.ToString()) != 0)
						{
							continue;
						}
					}
					this.m_CurrentSelectedItem = treeListViewItem;
					treeListViewItem.Selected = true;
					flag = true;
					break;
				}
			}
			if (!flag)
			{
				this.m_CurrentSelectedItem = null;
			}
		}

		private void btnOk_Click(object sender, EventArgs e)
		{
			List<Element> filteredElements = this.GetFilteredElements();
			this.m_Manager.ShowElement(filteredElements, this.ZoomToElements);
		}

		private void btnCancel_Click(object sender, EventArgs e)
		{
		}

		private void btnApply_Click(object sender, EventArgs e)
		{
			List<Element> filteredElements = this.GetFilteredElements();
			this.m_Manager.ShowElement(filteredElements, this.ZoomToElements);
		}

		private void rbFamilyType_CheckedChanged(object sender, EventArgs e)
		{
			if (this.rbFamilyType.Checked)
			{
				base.Width = (int)((double)this.treeListView.Width * 1.1) + (int)((double)this.btnSelAll.Width * 1.2);
				this.btnApply.Left = this.btnSelAll.Right - this.btnApply.Width;
				this.btnCancel.Left = this.btnApply.Left - (int)((double)this.btnCancel.Width * 1.2);
				this.btnOk.Left = this.btnCancel.Left - (int)((double)this.btnOk.Width * 1.2);
				this.columnHeader1.Text = "类别—族—类型";
			}
			else
			{
				base.Width = (int)((double)this.treeListView.Width * 1.1) + (int)((double)this.btnSelAll.Width * 1.2) + (int)((double)this.dbgPropertiesList.Width * 1.1);
				this.btnApply.Left = this.dbgPropertiesList.Right - this.btnApply.Width;
				this.btnCancel.Left = this.btnApply.Left - (int)((double)this.btnCancel.Width * 1.2);
				this.btnOk.Left = this.btnCancel.Left - (int)((double)this.btnOk.Width * 1.2);
				this.columnHeader1.Text = "类别—族—属性分组";
			}
			this.UpdateFilterTypeChange();
			this.UpdateSelectCount();
			this.UpdateCurrentSelectedItem();
		}

		private void treeListView_ItemChecked(object sender, ItemCheckedEventArgs e)
		{
			TreeListViewItem treeListViewItem = e.Item as TreeListViewItem;
			if (treeListViewItem.ChildrenCount != 0)
			{
				return;
			}
			ItemData itemDataByTreeNode = this.GetItemDataByTreeNode(treeListViewItem);
			if (itemDataByTreeNode != null)
			{
				itemDataByTreeNode.SetSelected(treeListViewItem.Checked);
			}
			this.UpdateSelectCount();
		}

		private void treeListView_SelectedIndexChanged(object sender, EventArgs e)
		{
			if (this.treeListView.SelectedItems.Count == 0)
			{
				return;
			}
			this.m_CurrentSelectedItem = this.treeListView.SelectedItems[0];
			this.UpdateSelectCount();
			if (this.rbFamilyType.Checked)
			{
				return;
			}
			if (this.m_CurrentSelectedItem.Level != 1)
			{
				this.UpdatePropertyList(-1, -1);
				return;
			}
			int categoryIndex = 0;
			try
			{
				categoryIndex = Convert.ToInt32(this.m_CurrentSelectedItem.Parent.Tag);
			}
			catch (Exception)
			{
				return;
			}
			int familyIndex = 0;
			try
			{
				familyIndex = Convert.ToInt32(this.m_CurrentSelectedItem.Tag);
			}
			catch (Exception)
			{
				return;
			}
			this.UpdatePropertyList(categoryIndex, familyIndex);
		}

		private void UpdateFilterTypeChange()
		{
			this.treeListView.Items.Clear();
			for (int i = 0; i < this.m_SelectCategoryList.Count; i++)
			{
				CategoryItemData categoryItemData = this.m_SelectCategoryList[i];
				TreeListViewItem treeListViewItem = TreeListViewEditor.AddRootItem(categoryItemData.TheCategory.Name, 0, categoryItemData.Selected, this.treeListView);
				TreeListViewEditor.AddColumnItem(categoryItemData.GetAllElements().Count.ToString(), treeListViewItem);
				treeListViewItem.Tag = i;
				for (int j = 0; j < categoryItemData.FamilyItemList.Count; j++)
				{
					FamilyItemData familyItemData = categoryItemData.FamilyItemList[j];
					TreeListViewItem treeListViewItem2 = TreeListViewEditor.AddItem(familyItemData.TheName, 0, familyItemData.Selected, treeListViewItem);
					TreeListViewEditor.AddColumnItem(familyItemData.GetAllElements().Count.ToString(), treeListViewItem2);
					treeListViewItem2.Tag = j;
					if (this.rbFamilyType.Checked)
					{
						for (int k = 0; k < familyItemData.TypeItemList.Count; k++)
						{
							TypeItemData typeItemData = familyItemData.TypeItemList[k];
							TreeListViewItem treeListViewItem3 = TreeListViewEditor.AddItem(typeItemData.TheName, 0, typeItemData.Selected, treeListViewItem2);
							treeListViewItem3.Tag = k;
							TreeListViewEditor.AddColumnItem(typeItemData.Elements.Count.ToString(), treeListViewItem3);
						}
					}
					else
					{
						for (int l = 0; l < familyItemData.GroupingDataList.Count; l++)
						{
							GroupingDataByProperty groupingDataByProperty = familyItemData.GroupingDataList[l];
							TreeListViewItem treeListViewItem4 = TreeListViewEditor.AddItem(groupingDataByProperty.TheName + "：" + groupingDataByProperty.TheValue, 0, groupingDataByProperty.Selected, treeListViewItem2);
							treeListViewItem4.Tag = l;
							TreeListViewEditor.AddColumnItem(groupingDataByProperty.Elements.Count.ToString(), treeListViewItem4);
						}
					}
				}
			}
		}

		private void UpdatePropertyList(int categoryIndex, int familyIndex)
		{
			this.dbgPropertiesList.Rows.Clear();
			if (-1 == categoryIndex || -1 == familyIndex)
			{
				return;
			}
			foreach (PropertyInfo propertyInfo in this.m_SelectCategoryList[categoryIndex].FamilyItemList[familyIndex].ThePropertyInfo)
			{
				DataGridViewRow dataGridViewRow = new DataGridViewRow();
				DataGridViewCheckBoxCell dataGridViewCheckBoxCell = new DataGridViewCheckBoxCell();
				dataGridViewCheckBoxCell.Value = propertyInfo.Checked;
				dataGridViewRow.Cells.Add(dataGridViewCheckBoxCell);
				DataGridViewTextBoxCell dataGridViewTextBoxCell = new DataGridViewTextBoxCell();
				dataGridViewTextBoxCell.Value = propertyInfo.TheName;
				dataGridViewRow.Cells.Add(dataGridViewTextBoxCell);
				this.dbgPropertiesList.Rows.Add(dataGridViewRow);
			}
		}

		private void UpdateSelectCount()
		{
			string str = this.GetFilteredElements().Count.ToString();
			this.lbSelCount.Text = "选定项总数：" + str;
		}

		private FilterType GetFilterType()
		{
			FilterType result = FilterType.FT_Type;
			if (this.rbFamilyProperty.Checked)
			{
				result = FilterType.FT_Proporty;
			}
			return result;
		}

		private ItemData GetItemDataByTreeNode(TreeListViewItem item)
		{
			ItemData result;
			if (item.Level == 0)
			{
				int index = Convert.ToInt32(item.Tag);
				result = this.m_SelectCategoryList[index];
			}
			else if (item.Level == 1)
			{
				int index2 = Convert.ToInt32(item.Parent.Tag);
				int index3 = Convert.ToInt32(item.Tag);
				result = this.m_SelectCategoryList[index2].FamilyItemList[index3];
			}
			else
			{
				int index4 = Convert.ToInt32(item.Parent.Parent.Tag);
				int index5 = Convert.ToInt32(item.Parent.Tag);
				if (this.rbFamilyType.Checked)
				{
					int index6 = Convert.ToInt32(item.Tag);
					result = this.m_SelectCategoryList[index4].FamilyItemList[index5].TypeItemList[index6];
				}
				else
				{
					int index7 = Convert.ToInt32(item.Tag);
					if (this.m_SelectCategoryList[index4].FamilyItemList[index5].GroupingDataList.Count == 0)
					{
					}
					result = this.m_SelectCategoryList[index4].FamilyItemList[index5].GroupingDataList[index7];
				}
			}
			return result;
		}

		public List<Element> GetFilteredElements()
		{
			List<Element> list = new List<Element>();
			FilterType filterType = this.GetFilterType();
			foreach (CategoryItemData categoryItemData in this.m_SelectCategoryList)
			{
				list.AddRange(categoryItemData.GetSelectElements(filterType));
			}
			return list;
		}

		private void dbgPropertiesList_CellValueChanged(object sender, DataGridViewCellEventArgs e)
		{
			if (this.m_CurrentSelectedItem == null)
			{
				return;
			}
			TreeListViewItem item;
			if (this.m_CurrentSelectedItem.Level == 1)
			{
				item = this.m_CurrentSelectedItem;
			}
			else
			{
				item = this.m_CurrentSelectedItem.Parent;
			}
			FamilyItemData familyItemData = this.GetItemDataByTreeNode(item) as FamilyItemData;
			int rowIndex = this.dbgPropertiesList.CurrentCell.RowIndex;
			bool isChecked = Convert.ToBoolean((this.dbgPropertiesList.CurrentCell as DataGridViewCheckBoxCell).Value.ToString());
			familyItemData.ChangePropertyInfo(rowIndex, isChecked);
		}

		private void treeListView_ItemMouseHover(object sender, ListViewItemMouseHoverEventArgs e)
		{
			if (base.CreateGraphics().MeasureString(e.Item.Text, new Font("宋体", 9f)).Width > 150f)
			{
				this.m_toolTip.Dispose();
				this.m_toolTip = new ToolTip();
				this.m_toolTip.Show(e.Item.Text, this.treeListView);
				this.m_toolTip.ShowAlways = true;
				return;
			}
			this.m_toolTip.Hide(this.treeListView);
		}

		private void treeListView_MouseLeave(object sender, EventArgs e)
		{
			if (this.m_toolTip != null)
			{
				this.m_toolTip.Dispose();
			}
		}

		private FilterManager m_Manager;

		private List<CategoryItemData> m_SelectCategoryList = new List<CategoryItemData>();

		private TreeListViewItem m_CurrentSelectedItem;

		private ToolTip m_toolTip = new ToolTip();
	}
}
